layui.use(['layer', 'form', 'ztree', 'laydate', 'admin', 'ax', 'table', 'func', 'dict', 'formSelects', 'xmSelect','upload'], function () {
    var form = layui.form;
    var $ax = layui.ax;
    var table = layui.table;
    var func = layui.func;
    var upload = layui.upload;
    var $ = jQuery = layui.jquery;
    var dict = layui.dict; //获取自定义模块
    var formSelects = layui.formSelects;
    var xmSelect = layui.xmSelect;
    var laydate = layui.laydate;
    dict.renderDictAll(); //渲染
    //table的初始化实例

    $('#spRange').val("USEMATERIALS");
    //重新渲染select数据
    form.render('select');

    //日期时间范围
    laydate.render({
        elem: '#purchaseTimeRange',
        range: true,
        format: 'yyyy/MM/dd'
    });

    /**
     * 系统管理--菜单管理
     */
    var Menu = {
        tableId: "menuTable",    //表格id
        condition: {
            menuId: "",
            menuName: "",
            level: ""
        }
    };
    //宽度大小
    var sm = 110;
    var nor = 150;
    var lg = 180;
    var bt = 220;
    /**
     * 初始化表格的列
     */
    Menu.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'orderCode', sort: true, title: '订单编号'},
            {field: 'dishName', sort: true, title: '菜品名称'},
            {field: 'orderPrice', sort: true, title: '订单金额'},
            {field: 'orderContent', sort: true, title: '订单内容'},
            {field: 'orderEvaluate', sort: true, title: '用户评价'},
            {field: 'orderPlatform', sort: true, title: '订单平台', templet: function (data) {
                    return data.orderPlatform === 'MT' ? '美团' : '饿了么';
                }},
            {field: 'orderCreateDate', sort: true, title: '订单日期'},
            {field: 'memberName', sort: true, title: '用户名'},
            {field: 'memberPhone', sort: true, title: '用户手机号'},
            {field: 'memberAddress', sort: true, title: '用户下单地址'},
            {field: 'orderScore', sort: true, title: '订单评分'}

        ]];
    };


    Menu.initTable = function(queryData) {
        var path = '/erya/orderDetail/queryAll';
        if (queryData) {
            path += '?' + queryData;
        }
        return table.render({
            elem: '#' + Menu.tableId,
            url: Feng.ctxPath + path,
            page: true,
            height: "full-98",
            cellMinWidth: 100,
            cols: Menu.initColumn(),
        });
    }

    // 搜索
    $('#btnQuery').click(function () {
        var orderChannel = $('#orderChannel').val().trim();
        var purchaseTimeRange = $('#purchaseTimeRange').val().trim();
        Menu.initTable('orderChannel=' +　orderChannel + '&purchaseTimeRange=' + purchaseTimeRange);
    });

    Menu.initTable(null);

    /**
     * 点击编辑菜单按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    Menu.onEditMenu = function (data) {
        func.open({
            height: 720,
            title: '修改菜单',
            content: Feng.ctxPath + "/banjiang/sp_edit?spNo=" + data.sp_no,
            tableId: Menu.tableId,
            endCallback: function () {
                table.reload(Menu.tableId);
            }
        });
    };

    $('#delete').click(function () {
        Menu.deleteData();
    });

    Menu.deleteData = function () {
        var dataArray = layui.table.checkStatus('menuTable').data;
        if (dataArray.length == 0) {
            Feng.error("请选择需要删除的数据！")
            return;
        }
        var id = '';
        for (const data of dataArray) {
            id += (data.id + ',')
        }
        var operation = function () {
            $.ajax({
                url: Feng.ctxPath + "/erya/orderDetail/deleteData",
                type: "post", // 请求类型
                data: {"orderIds": id},
                dataType: 'json',
                async: true, // 是否异步
                success: function (data) {
                    Feng.success("操作成功！");
                    table.reload(Menu.tableId);
                },
                error: function (data) {
                    Feng.error("操作失败！" + data.responseJSON.message)
                }
            });
        };
        Feng.confirm("是否确认删除?", operation);
    }



    $('#btnExport').click(function () {
        Menu.exportMenu();
    });

    $('#read').click(function () {
        Menu.readMessage();
    });

    Menu.readMessage = function () {
        var dataArray = layui.table.checkStatus('menuTable').data;
        var id = '';
        for (const data of dataArray) {
            id += (data.id + ',')
        }
        $.ajax({
            url: Feng.ctxPath + "/erya/message/readMessage",
            type: "post", // 请求类型
            data: {"id": id},
            dataType: 'json',
            async: true, // 是否异步
            success: function (data) {
                Feng.success("操作成功！");
                table.reload(Menu.tableId);
            },
            error: function (data) {
                Feng.error("操作失败！" + data.responseJSON.message)
            }
        });
    }


    // 工具条点击事件
    table.on('tool(menuTable)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Menu.onEditMenu(data);
        } else if (layEvent === 'delete') {
            Menu.onDeleteMenu(data);
        } else if (layEvent === 'spShopAssign') {
            Menu.spShopAssign(data);
        } else if (layEvent === 'copy') {
            Menu.onCopyMenu(data);
        } else if (layEvent === 'limit') {
            Menu.openLimitMenu(data);
        }
    });

    // 执行实例
    upload.render({
        elem: '#btnExp',
        url: '/erya/orderDetail/uploadExcel',
        accept: 'file',
        acceptMime: 'application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        exts: 'xlsx|xls',
        before: function(obj){
            layer.load(0, {
                shade: [0.3, '#393D49'],
            }); //上传loading
        },
        done: function (res) {
            layer.closeAll('loading'); //关闭loading
            if (res.code === 0) {
                Feng.info(res.message)
                Menu.initTable();
            } else {
                Feng.error('导入进货数据失败！' + res.message)
            }
        }
    });

    $('#downloadExcel').click(function () {
        window.location.href=Feng.ctxPath + '/erya/orderDetail/downLoad';
    });

});